<?
function tambah(){
	global $inp_par,$inp,$permisi,$sql_tambahan,$cdivisi_kode;
	global $waktu_update,$tgl_update,$ip_update,$cnama,$cdivisi_name;
        $ip = e_getIpAddr();
        $valid = "SELECT * FROM tbl_user WHERE user_name='".$inp[user_name]."'";
        $resvalid = dbresult($valid);
        $rs = pg_fetch_array($resvalid);
        if ($rs==""){
            $sql = "insert into tbl_user(user_name,password,first_name,last_name,jabatan,kode_entitas,nip,level_akses,user_update,tgl_update,ip_update,divisi_update,waktu_update)  values ('$inp[user_name]','$inp[password]','$inp[first_name]','$inp[last_name]','$inp[jabatan]','$inp[divisi_kode]','$inp[nip]','$inp[level]','$cnama','$tgl_update','$ip','$cdivisi_name','$waktu_update')";
            dbresult($sql);//echo $sql;
            echo "<script>window.location='?".var_kirim($inp_par,"mode")."';</script>";
        }else{
            echo "<script>alert('Username sudah ada Di Database.. !!')</script>";
            echo "<script>window.history.back();</script>";
        }
}

function hapus(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cdivisi_kode;
	global $waktu_update,$tgl_update,$ip_update;
        $sql = "delete from tbl_user where user_id='".trim($inp_par[user_id])."'";
        dbresult($sql);
        echo "<script>window.location='?".var_kirim($inp_par,"mode")."';</script>";
}

function ubah(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cdivisi_kode;
	global $waktu_update,$tgl_update,$ip_update,$cnama,$cdivisi_name;
        $ip = e_getIpAddr();
        $sql = "update tbl_user set user_name='$inp[user_name]',password='$inp[password]',first_name='$inp[first_name]',last_name='$inp[last_name]',jabatan='$inp[jabatan]',kode_entitas='$inp[divisi_kode]',nip='$inp[nip]',level_akses='$inp[level]',user_update='$cnama',tgl_update='$tgl_update',ip_update='$ip',divisi_update='$cdivisi_name',waktu_update='$waktu_update' where user_id='$inp_par[user_id]'";
        $result=dbresult($sql);
        echo "<script>window.location='?".var_kirim($inp_par,"mode")."';</script>";
}

function form(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cdivisi_kode;
        global $divisi_kode,$div_kode;
	global $waktu_update,$tgl_update,$ip_update;
	
	if($inp_par[mode]=="edit"){
		$sql="select * from tbl_user where user_id='$inp_par[user_id]'";
		$result = dbresult($sql);//echo $sql;
		$r = pg_fetch_array($result, 0);
	}
        $r[kode_entitas]=$r[kode_entitas]==""?$cdivisi_kode:$r[kode_entitas];

        generate_rule("is_isi","inp[user_name]","Anda Harus Mengisi User Name");
	generate_rule("is_isi","inp[password]","Anda Harus Mengisi Password");
	generate_rule("is_isi","inp[first_name]","Anda Harus Mengisi First Name");
        generate_rule("is_isi","inp[last_name]","Anda Harus Mengisi Last Name");
	generate_rule("is_isi","inp[JABATAN]","Anda Harus Mengisi Jabatan");
        generate_rule("is_isi","inp[nip]","Anda Harus Mengisi Nip");
        generate_rule("is_isi","inp[divisi_kode]","Anda Harus Mengisi Unit Usaha");
        generate_rule("is_isi","inp[level]","Anda Harus Mengisi Level Akses");


        $txtout=generate_validasi();
        $txtout.="<form name=form1 method=\"post\" action=\"?".var_kirim($inp_par)."\" onSubmit=\"return validasi(document.form1);\">
		<table width=700 border=0 align=center cellpadding=3 cellspacing=1 bgcolor=\"C6E6FF\" class=tabel1>
		<tr class=\"tbltagihan\">
		<td colspan=\"3\">Form Input User</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>1.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">USER NAME</td>
		<td bgcolor=\"#FFFFFF\"><input name=\"inp[user_name]\" type=\"text\" class=\"form2\" value=\"".trim($r["user_name"])."\"></td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>2.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">PASSWORD</td>
		<td><input name=\"inp[password]\" type=\"password\" class=\"form2\" id='inputPC' autocomplete='off' onKeyUp='EvalPwdStrength(document.forms[0],this.value);' value=\"".trim($r["password"])."\"></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>&nbsp;</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">&nbsp;</td>
		<td><table width=\"80%\" cellpadding='0' cellspacing='0' class='pwdChkTbl2'><tbody><tr><td id='idSM1' width='80&amp;percnt;' class='pwdChkCon0' align='center'><span style='font-size:1px'>&nbsp;</span><span id='idSMT1' style='display: none; '>Weak</span></td><td id='idSM2' width='80&amp;percnt;' class='pwdChkCon0' align='center' style='border-left:solid 1px #fff'><span style='font-size:1px'>&nbsp;</span><span id='idSMT0' style='font-weight: normal; color: rgb(102, 102, 102); display: inline; '>Not rated</span><span id='idSMT2' style='display: none; '>Medium</span></td><td id='idSM3' width='80&amp;percnt;' class='pwdChkCon0' align='center' style='border-left:solid 1px #fff'><span style='font-size:1px'>&nbsp;</span><span id='idSMT3' style='display: none; '>Strong</span></td><td id='idSM4' width='80&amp;percnt;' class='pwdChkCon0' align='center' style='border-left:solid 1px #fff'><span style='font-size:1px'>&nbsp;</span><span id='idSMT4' style='display: none; '>BEST</span></td></tr></tbody></table></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>3.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">RETYPE PASSWORD</td>
		<td><div style='float:left'><input class=\"form2\" type=\"password\" name=\"inp[password1]\" id='retype' size=30 onKeyUp='cekretype();'>&nbsp;&nbsp;&nbsp;</div><div id='cekretype'></div></td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>4.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">FIRST NAME</td>
		<td><input onkeyup='ToUpperFirst(this)' name=\"inp[first_name]\" type=\"text\" class=\"form2\" value=\"".trim($r["first_name"])."\"></td></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>5.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">LAST NAME</td>
		<td><input onkeyup='ToUpperFirst(this)' name=\"inp[last_name]\" type=\"text\" class=\"form2\" value=\"".trim($r["last_name"])."\"></td></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>6.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">JABATAN</td>
		<td><input onkeyup='ToUpper(this)' name=\"inp[jabatan]\" type=\"text\" class=\"form2\" value=\"".trim($r["jabatan"])."\"></td></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>7.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">NIP</td>
		<td><input name=\"inp[nip]\" type=\"text\" class=\"form2\" value=\"".trim($r["nip"])."\"></td></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>8.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">UNIT USAHA</td>
		<td>".combo("select divisi_kode,divisi_name from tbl_divisi where true","divisi_kode","divisi_name","inp[divisi_kode]","","$r[kode_entitas]","t")."</td></td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td width=30> <div align=center>9.</div></td>
		<td width=130 bgcolor=\"#FFFFFF\">LEVEL AKSES</td>
		<td>".combo("tbl_peran","id_peran","nama_peran","inp[level]","",$r[level_akses])."</td></td>
		</tr>";
	
	$txtout.="<tr bgcolor=\"#FFFFFF\">
		<td colspan=\"3\"> <div align=\"center\">";
        if($permisi["add"]=="izin"){
                $txtout.="<span id='tombolsave' ></span>";
        }
	$txtout.="&nbsp;<input type=\"button\" name=\"Submit2222222222\" value=\"Cancel &raquo;\" class=\"tombolgo\" onClick=\"javascript:history.back()\">
		</div></td>
		</tr>
		</table>
		</form>";
	return $txtout;
}

function detail(){
	global $inp_par,$inp;
	$sql="select * from tbl_user where user_id='$inp_par[user_id]'";
	$res=dbresult($sql);
	$r=pg_fetch_array($res);
	
	$divisi=cari_nilai("select divisi_name from tbl_divisi where divisi_kode='$r[kode_entitas]'");
        $peran=cari_nilai("select nama_peran from tbl_peran where id_peran='$r[level_akses]'");
	$txtout.="<table width=\"98%\" height=\"35\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
		<tr>
		<td height=\"5\" colspan=\"2\"><img src=\"../img/0.gif\" width=\"1\" height=\"10\"></td>
		</tr>
		</table>
		<br>

		<table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"FBCB7E\" class=\"tabel1\">
		<form name=form1 method=post action=\"?".var_kirim($inp_par)."\" onsubmit=\"return validasi(document.form1);\">
		<tr class=\"tbltagihan\">
		<td colspan=\"3\"> Detail User</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td width=\"30\"> <div align=\"center\">1.</div></td>
		<td width=\"150\">USER NAME</td>
		<td>$r[user_name]</td>
                <tr bgcolor=\"#FFFFFF\">
		<td> <div align=\"center\">2.</div></td>
		<td>FIRST NAME</td>
		<td>$r[first_name]</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td> <div align=\"center\">3.</div></td>
		<td>LAST NAME</td>
		<td>$r[last_name]</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td> <div align=\"center\">4.</div></td>
		<td>JABATAN</td>
		<td>$r[jabatan]</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td> <div align=\"center\">5.</div></td>
		<td>NIP</td>
		<td>$r[nip]</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td>
		<div align=\"center\">6.</div></td>
		<td>UNIT USAHA</td>
		<td>$divisi</td>
		</tr>
                <tr bgcolor=\"#FFFFFF\">
		<td>
		<div align=\"center\">7.</div></td>
		<td>LEVEL AKSES</td>
		<td>$peran</td>
		</tr>
		<tr bgcolor=\"#FFFFFF\">
		<td colspan=\"3\"> <div align=\"right\">
		<input type=\"button\" name=\"Submit2222222222\" value=\"Close &raquo;\" class=\"tombolgo\" onClick=\"window.close();\">
		</div></td>
		</tr>
		</table>";
	return $txtout;
}

function lihat(){
    global $inp_par,$inp,$permisi,$sql_tambahan;
	$txtout.="<form name=form_cr method=post action=\"?\">
		<table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"hitam\">
		<tr>
		<td>Search :&nbsp;</td>
		<td>
		<input name=\"inp_par[txtcari]\" type=\"text\" class=\"form1\" size=\"30\" value=\"$inp_par[txtcari]\">
		</td>
		<td>&nbsp;<input type=submit name=\"go\" value=\"GO &raquo;\" class=\"tombolgo\"></td>
		</tr>
		</table>
		</form>";

	//      print_r($inp_par);
	$kata=strtolower(trim($inp_par[txtcari]));
	if($kata<>""){
		$sql_tambahan .= " where (lower(user_name) like('%".$kata."%')";
		$sql_tambahan .= " or lower(first_name) like('%".$kata."%')";
		$sql_tambahan .= " or lower(last_name) like('%".$kata."%')";
                $sql_tambahan .= " or lower(jabatan) like('%".$kata."%')";
                $sql_tambahan .= " or lower(nip) like('%".$kata."%')";
		$sql_tambahan .= " or lower(kode_entitas) like('%".$kata."%'))";
	}

	$sql1="select *  from qry_user $sql_tambahan";
	$arr_tabel=definisi_halaman($sql1);
	$result1=$arr_tabel["res_sql"];
	$i=1+$inp_par[awalan]*$inp_par[jumlah_per_hal];

	$txtout.="<form name=\"form2\" method=\"post\">
		<table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#C6E6FF\" class=\"tabel1\">
		<tr class=\"tbltagihan\">
		<td width=\"20\"><div align=\"center\">No</div></td>
		<td><div align=\"center\">USER ID</div></td>
		<td width=\"120\"><div align=\"center\">FIRST NAME</div></td>
		<td width=\"120\"><div align=\"center\">LAST NAME</div></td>
		<td width=\"150\"><div align=\"center\">JABATAN</div></td>
                <td width=\"90\"><div align=\"center\">NIP</div></td>
                <td width=\"90\"><div align=\"center\">UNIT USAHA</div></td>
                <td width=\"90\"><div align=\"center\">LEVEL AKSES</div></td>
		<td width=\"60\"><div align=\"center\">Aksi</td>
		</tr>";

	while($r1=pg_fetch_array($result1)){
		if($warna=="#E3F1FC"){
			$warna="#F4FAFF";
		}else{
			$warna="#E3F1FC";
		}
                $level_akses=cari_nilai("select nama_peran from tbl_peran where id_peran='$r1[level_akses]'");
		$txtout.="
			<tr onmouseover=\"this.style.backgroundColor='#FDFFDD';\" style=\"background-color: $warna;\" onmouseout=\"this.style.backgroundColor='$warna';\">
			<td><div align=\"center\">$i.</div></td>
			<td><a href=\"javascript:Start('?inp_par[mode]=detail&inp_par[user_id]=$r1[user_id]',500,400)\" class=\"tiga\">$r1[user_name]</td>
			<td>$r1[first_name]</td>
			<td>$r1[last_name]</td>
			<td>$r1[jabatan]</td>
                        <td>$r1[nip]</td>
                        <td>$r1[kode_entitas]</td>
                        <td>$level_akses</td>
			<td width=60><div align=\"center\">";

		if($permisi["edit"]=="izin"){
			$txtout.="<a href=\"?inp_par[mode]=edit&inp_par[user_id]=$r1[user_id]".var_kirim($inp_par,"user_id")."\" title=\"Ubah Data\" class=\"edit\">Edit</a>";
		}
		if($permisi["delete"]=="izin"){
			$txtout.=" | <a href=\"?inp_par[mode]=delete&inp_par[user_id]=$r1[user_id]".var_kirim($inp_par,"user_id")."\" onclick=\"return confirm('Anda Yakin Akan Menghapus Data Ini??')\"  title=\"Hapus Data\" class=\"delete\">Delete</a>";
		}

		$txtout.=" </div></td></tr>";
		$i++;
	}
	$txtout.="</table>
		<table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tbltagihan\">
		<tr>
		<td>&nbsp;";
	if($permisi["add"]=="izin"){
		$txtout.=" <input type=\"button\" name=\"Submit3\" value=\"Tambah Data &raquo;\" class=\"tomboladd\" onclick='javascript:window.location=\"?inp_par[mode]=add".var_kirim($inp_par,"mode")."\";'>";
	}

	$txtout.="</td>
		<td>$arr_tabel[tabel_halaman]</td>
		</tr>
		</table>
                </form>";
	//End Tampilan Data//

	return $txtout;
}

function generate_form($inp_par){
        global $inp_par,$inp;
	switch ($inp_par[mode]){
		case "add":
			if($inp[aksi]==""){
				$txtout.= form();
			}else{
				$txtout.= tambah();

			}
		break;
		case "edit":
			if($inp[aksi]==""){
				$txtout.= form();
			}else{
				$txtout.= ubah();
			}
		break;
		case "detail":
			$txtout.= detail();
		break;
		case "delete":
			$txtout.= hapus();
		break;
		default:
		$txtout.=lihat();
		break;
	}
	return $txtout;
}

?>
